#include "stdafx.h"
#include "TG_DecSDK.h"

#include "TGVideoDecMan.h"
#include "TGAudioDecMan.h"


DWORD __stdcall TG_Dec_GetSdkVersion()
{
	return 0;
}

TGERR_CODE  __stdcall TG_VideoDec_Init(HANDLE &hDecoder)
{
	TGVideoDecMan *pDec = new TGVideoDecMan;
	if( NULL != pDec )
	{
		hDecoder = (HANDLE)pDec;
		return TG_ERR_SUCCESS;
	}

	return TG_ERR_FAILURE;
}

TGERR_CODE  __stdcall TG_VideoDec_Decode(HANDLE hDecoder,BYTE* pEncFrame,long lEncSize,TG_VIDEO_DEC_INFO *pOutDecInfo)
{
	TGVideoDecMan *pDec = (TGVideoDecMan*)hDecoder;
	if( NULL != pDec )
	{
       return (TGERR_CODE)pDec->Decode(pEncFrame,lEncSize,pOutDecInfo);
	}

	return TG_ERR_FAILURE;
}

TGERR_CODE  __stdcall TG_VideoDec_Release(HANDLE hDecoder)
{
	TGVideoDecMan *pDec = (TGVideoDecMan*)hDecoder;
	if( NULL != pDec )
	{
		delete pDec;
		pDec = NULL;
	}
	return TG_ERR_SUCCESS;
}

TGERR_CODE  __stdcall TG_AudioDec_Init(HANDLE &hDecoder)
{
	TGAudioDecMan *pDec = new TGAudioDecMan;
	if( NULL != pDec )
	{
		hDecoder = (HANDLE)pDec;
		return TG_ERR_SUCCESS;
	}

	return TG_ERR_FAILURE;
}

TGERR_CODE  __stdcall TG_AudioDec_Decode(HANDLE hDecoder,BYTE* pEncFrame,long lEncSize,TG_AUDIO_DEC_INFO *pOutDecInfo)
{
	TGAudioDecMan *pDec = (TGAudioDecMan*)hDecoder;
	if( NULL != pDec )
	{
		return (TGERR_CODE)pDec->Decode(pEncFrame,lEncSize,pOutDecInfo);
	}

	return TG_ERR_FAILURE;
}

TGERR_CODE  __stdcall TG_AudioDec_Release(HANDLE hDecoder)
{
	TGAudioDecMan *pDec = (TGAudioDecMan*)hDecoder;
	if( NULL != pDec )
	{
		delete pDec;
		pDec = NULL;
	}
	return TG_ERR_SUCCESS;
}

TGERR_CODE  __stdcall TG_AudioEnc_Init(HANDLE &hEncoder)
{
	TGAudioDecMan *pEnc = new TGAudioDecMan;
	if( NULL != pEnc )
	{
		hEncoder = (HANDLE)pEnc;
		return TG_ERR_SUCCESS;
	}

	return TG_ERR_FAILURE;
}

TGERR_CODE  __stdcall TG_AudioEnc_Encode(HANDLE hEncoder,TG_AUDIO_DEC_INFO *pInPcmInfo,unsigned long lTimeStamp,BYTE** ppOutEncFrame,long *lpOutEncSize)
{
	TGAudioDecMan *pEnc = (TGAudioDecMan*)hEncoder;
	if( NULL != pEnc )
	{
		return (TGERR_CODE)pEnc->Encode(pInPcmInfo,lTimeStamp,ppOutEncFrame,lpOutEncSize);
	}

	return TG_ERR_FAILURE;
}

TGERR_CODE  __stdcall TG_AudioEnc_Release(HANDLE hEncoder)
{
	TGAudioDecMan *pEnc = (TGAudioDecMan*)hEncoder;
	if( NULL != pEnc )
	{
		delete pEnc;
		pEnc = NULL;
	}
	return TG_ERR_SUCCESS;
}

#include "TG_EX_NAL_Def.h"

TGERR_CODE  __stdcall TG_PickPIC(BYTE *pFrame,long lFrameSize,BYTE **ppPicBuffer,long &lPicSize)
{
	*ppPicBuffer = NULL;
	lPicSize = 0;
	if( NULL == pFrame || lFrameSize < 33 )
	{
		return TG_ERR_FAILURE;
	}

	TG_FRAME_HEAD *pFrameHead = (TG_FRAME_HEAD*)pFrame;

	UINT16 nOffset = pFrameHead->cHeadSize;

	TG_NAL_HEAD *pNalHead = NULL;
	BYTE         *pNalData = NULL;

	int nRet = 0;

	unsigned int    len  = pFrameHead->nFrameSize;
	unsigned char * data = pFrame + nOffset;

	//unsigned encodeType = -1;
	unsigned nalType = 0;

	while( len > 5 )
	{
		nalType = data[3] & 0x1f;
		if(data[0] == 0 && data[1] == 0 && data[2] == 1 && nalType == NAL_TYPE_EX_PIC )
		{
			//encodeType = data[4];
			data += 5;
			len -= 5;

			*ppPicBuffer = data;
			lPicSize     = len;

			return TG_ERR_SUCCESS;
		}

		data += 1;
		len -= 1;
	}


	return TG_ERR_FAILURE;
}

#include "MVCodec.h"
TGERR_CODE  __stdcall TG_CreateMV_Init(HANDLE &hHandle)
{
	MVCodec *pCodec = new MVCodec;
	if( NULL != pCodec )
	{
		hHandle = (HANDLE)pCodec;
		return TG_ERR_SUCCESS;
	}

	return TG_ERR_FAILURE;
}

TGERR_CODE  __stdcall TG_CreateMV_Create(HANDLE hHandle,TG_MV_SRC *pMVSrc,void **ppFrame,long &lFrameSize)
{
	MVCodec *pCodec = (MVCodec*)hHandle;
	if( NULL != pCodec )
	{
		return (TGERR_CODE)pCodec->Create(pMVSrc,ppFrame,lFrameSize);
	}

	return TG_ERR_FAILURE;
}


TGERR_CODE  __stdcall TG_CreateMV_Release(HANDLE hHandle)
{
	MVCodec *pCodec = (MVCodec*)hHandle;
	if( NULL != pCodec )
	{
		delete pCodec;
		pCodec = NULL;
	}
	return TG_ERR_SUCCESS;
}

TGERR_CODE  __stdcall TG_PickMV(BYTE *pFrame,long lFrameSize,TG_MV_SRC *pMVSrc)
{
	return (TGERR_CODE)MVCodec::PickMV(pFrame,lFrameSize,pMVSrc);
}